package com.qen.fx.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.fx.custom.ContentHeader;
import com.qen.fx.custom.CustomBusiness;
import com.qen.fx.custom.CustomContent;
import com.qen.fx.custom.impl.DefaultBusiness;
import com.qen.fx.custom.impl.pwd.TokenCreate;
import com.qen.fx.tools.MenuTools;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

public class CustomSideController implements Initializable {

    private static HBox SelectHBox = null;
    public static String SideMenuId = null;

    @FXML
    private AnchorPane sidePane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void initCustomSide() {
        sidePane.getChildren().add(initAnchorPane());
    }

    private AnchorPane initAnchorPane() {
        Accordion accordion = new Accordion(init());
        AnchorPane pane = new AnchorPane(accordion);
        pane.setPrefHeight(ViewIndex.sidePreferHeight);
        pane.setPrefWidth(-1);
        return pane;
    }

    private TitledPane[] init() {
        JSONArray menus = MenuTools.getMenus();
        int mLen = menus.size();
        TitledPane[] panes = new TitledPane[mLen];
        for (int i = 0; i < mLen; i++) {
            JSONObject menu = menus.getJSONObject(i);
            JSONArray children = menu.getJSONArray("children");
            TitledPane titledPane = new TitledPane();
            titledPane.setAnimated(false);
            titledPane.setText(menu.getString("title"));
            titledPane.setId(menu.getString("id"));
            int len = children.size();
            HBox[] hBoxes = new HBox[len];
            for (int j = 0; j < len; j++) {
                JSONObject child = children.getJSONObject(j);

                ImageView imageView = new ImageView();
                imageView.setFitHeight(ViewIndex.sideHBoxHeight);
                imageView.setFitWidth(ViewIndex.sideHBoxHeight);
                imageView.setPickOnBounds(true);
                imageView.setPreserveRatio(true);
                InputStream icon = App.class.getResourceAsStream(child.getString("icon"));
                if (icon != null) {
                    imageView.setImage(new Image(icon));
                }
                HBox imgBox = new HBox();
                Insets insets = new Insets(0, 5, 0, 0);
                imgBox.setMargin(imageView, insets);

                Label label = new Label(child.getString("title"));
                label.setPrefHeight(ViewIndex.sideHBoxHeight);
                label.setPrefWidth(ViewIndex.sideLabelWidth);

                Node[] nodes = new Node[]{imageView, label};
                HBox hBox = new HBox(nodes);
                hBox.setPrefHeight(ViewIndex.sideHBoxHeight);
                hBox.setPrefWidth(ViewIndex.sideHBoxWidth);
                String id = child.getString("id");
                hBox.setId(child.getString("id"));
                hBox.setCursor(Cursor.HAND);
                String action = child.getString("action");
                hBox.setOnMouseClicked(e -> {
                    if (!id.equals(SideMenuId)) {
                        SideMenuId = id;
                        if (SelectHBox != null) {
                            SelectHBox.setStyle(null);
                        }
                        SelectHBox = hBox;
                    }
                    String titleText = child.getString("title");
                    String contentText = child.getString("content");
                    boolean isLike = child.getIntValue("like") == 1;

                    hBox.setStyle("-fx-background-color: #EAEAEA;");

                    CustomBusiness customBusiness = new DefaultBusiness();
                    switch (action) {
                        case "tokenCreate":
                            customBusiness = new TokenCreate();
                            break;
                        default:
                            customBusiness = new DefaultBusiness();
                            break;
                    }
                    ContentHeader contentHeader = new ContentHeader(titleText, contentText, isLike);
                    CustomContent customContent = new CustomContent(contentHeader, customBusiness);

                });
                hBox.setOnMouseDragged(e -> {
                });
                hBox.setOnMouseEntered(e -> {
                    hBox.setStyle("-fx-background-color: #EEE;");
                });
                hBox.setOnMouseExited(e -> {
                    if (hBox != SelectHBox) {
                        hBox.setStyle(null);
                    }
                });
                hBox.setOnMouseMoved(e -> {
                });
                hBox.setOnMouseReleased(e -> {
                });
                hBoxes[j] = hBox;
            }

            VBox vBox = new VBox(hBoxes);
            vBox.setLayoutX(ViewIndex.sideVBoxPadding);
            vBox.setPrefHeight(len * ViewIndex.sideHBoxHeight + ViewIndex.sidePreferAddHeight);
            vBox.setPrefWidth(ViewIndex.sidePreferWidth);

            AnchorPane anchorPane = new AnchorPane(vBox);
            anchorPane.setPrefHeight(len * ViewIndex.sideHBoxHeight + ViewIndex.sidePreferAddHeight);
            anchorPane.setPrefWidth(ViewIndex.sidePreferWidth);
            anchorPane.setMinHeight(0);
            anchorPane.setMinWidth(0);
            anchorPane.setOpacity(0.75);
            titledPane.setContent(anchorPane);
            panes[i] = titledPane;
        }
        return panes;
    }
}
